/*
 * MapReferencePointDialog.java
 *
 * Created on 1 mai 2007, 17:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package courselog.maputils;

import courselog.gpsutils.GeoPoint;
import courselog.gpsutils.PositionTracer;
import courselog.gpsutils.SerialGPSConnection;
import ewe.fx.Color;
import ewe.fx.Dimension;
import ewe.sys.Device;
import ewe.ui.CellPanel;
import ewe.ui.CheckBoxGroup;
import ewe.ui.Container;
import ewe.ui.Control;
import ewe.ui.ControlEvent;
import ewe.ui.Form;
import ewe.ui.mButton;
import ewe.ui.mCheckBox;
import ewe.ui.mInput;
import ewe.ui.mLabel;
import ewe.ui.mTextPad;

/**
 *
 * @author rigal
 */
public class MapReferencePointDialog extends Form {
    
    public static final String[] tstInfo=new String[]{
        "Sets latitude longitude of this point to current GPS position.",
        "Enter Latitude and longitude in the form +Deg Min Sec.dec\n"+
                "Latitude is positive North, longitude is positive East"};
    
    /** Parent Form that kaunches this dialog */
    public Container fParent=null;
    
    /** Position tracer of the application. */
    public PositionTracer myPositionTracer=null;
    
    public CheckBoxGroup cbgChoice=new CheckBoxGroup();
    
    public mCheckBox mchkPosition=new mCheckBox("Current GPS position");
    public mCheckBox mchkValues=new mCheckBox("Type in lat/lon values");
    
    public mLabel mlLat=new mLabel("Lat: ");
    public mLabel mlLon=new mLabel("Lon: ");
    public mTextPad mtInfo=new mTextPad();
    
    public mInput miLat=new mInput("                 ");
    public mInput miLon=new mInput("                 ");
    
    public double dVLat=courselog.gpsutils.GeoPoint.dummyAng;
    public double dVLon=courselog.gpsutils.GeoPoint.dummyAng;
    
    /** Creates a new instance of MapReferencePointdialog */
    public MapReferencePointDialog(Container parent) {
        fParent=parent;
        
        setTitle("Input location coordinates");
        mchkPosition.setGroup(cbgChoice);
        mchkValues.setGroup(cbgChoice);
        
        mtInfo.setAsStaticDisplay(true);
        
        CellPanel cpVal=new CellPanel();
        
        addLast(mchkPosition);
        
        cpVal.addLast(mchkValues);
        cpVal.addNext(mlLat);
        cpVal.addLast(miLat);
        cpVal.addNext(mlLon);
        cpVal.addLast(miLon);
        
        addLast(cpVal);
        
        addLast(mtInfo);
        
        //Gui.setOKCancel(this);
        if(this.ok==null) {
            this.ok=new mButton("OK","res/iconok.bmp",Color.White);
            addNext(this.ok);
        }
        
        if(this.cancel==null) {
            this.cancel=new mButton("CANCEL","res/iconcancel.bmp",Color.White);
            addLast(this.cancel);
        }
        
        Dimension dimP= new Dimension();
        Container par=getParent();
        if(par==null) {
            dimP=Device.getScreenSize();
        }else{
            dimP=par.getSize(dimP);
        }
        
        cpVal.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.2d));
        mtInfo.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.4d));
        
        updateTracer();
        setDialogFromIndex();
        
        //CourseLogAppForm.dbgL.addToLog(" Ok="+this.ok);
    }
    
    // ACCESSORS
    
    /**
     * Set the position tracer for this dialog.
     */
    public void setMyPositionTracer(PositionTracer pT) {
        myPositionTracer=pT;
        updateTracer();
    }
    
    /** Set Position */
    public void setPosition(GeoPoint pt) {
        dVLat=pt.lat;
        dVLon=pt.lon;
        double dV= dVLat;
        int iV=(int)dV;
        String msg=Integer.toString(iV);
        if(dV<0) {
            dV=-dV;
            iV=-iV;
        }
        dV -= iV;
        dV *=60.0d;
        iV = (int) dV;
        msg = msg+" "+iV;
        dV -= iV;
        dV *=60.0d;
        msg = msg+" "+dV;
        miLat.setText(msg);
        
        dV= dVLon;
        iV=(int)dV;
        msg=Integer.toString(iV);
        if(dV<0) {
            dV=-dV;
            iV=-iV;
        }
        dV -= iV;
        dV *=60.0d;
        iV = (int) dV;
        msg = msg+" "+iV;
        dV -= iV;
        dV *=60.0d;
        msg = msg+" "+dV;
        miLon.setText(msg);
    }
    
    // CLASS METHODS
    
    /**
     * Set the chekcbox according to the position tracer status.
     */
    public void updateTracer() {
        if(myPositionTracer!=null) {
            if(myPositionTracer.myGPS.status==SerialGPSConnection.STATUS_OK) {
                if(cbgChoice.getSelectedIndex()==0) {
                    setPosition(myPositionTracer.myCurrent);
                }
            }
            mchkPosition.modify(0,Control.Disabled);
//            cbgChoice.selectIndex(0);
        }else{
            mchkPosition.modify(Control.Disabled,0);
            cbgChoice.selectIndex(1);
        }
    }
    
    /**
     * Set dialog elements according to the selected choice.
     */
    public void setDialogFromIndex() {
        int idxC=cbgChoice.getSelectedIndex();
        mtInfo.setText(tstInfo[idxC]);
        if(idxC==1) {
            miLat.modify(0,Control.Disabled);
            miLon.modify(0,Control.Disabled);
        }else{
            updateTracer();
            miLat.modify(Control.Disabled,0);
            miLon.modify(Control.Disabled,0);
        }
    }
    
    /**
     * Compute Latitude from entered value.
     */
    public double getLatitudeValue() {
        return dVLat;
    }
    
    public boolean convertValOK() {
        try {
            String stVal=miLat.getText().trim();
            //CourseLogAppForm.dbgL.addToLog(" lat :"+stVal);
            double dVal = 0.0d;
            double dSign = 1.0d;
            int idx1=stVal.indexOf(' ');
            if((idx1>0)||(stVal.length()>0)) {
                if(idx1<=0) {
                    idx1=stVal.length();
                }
                dVal+= Integer.parseInt(stVal.substring(0,idx1));
                if(dVal<0.0d) {
                    dSign=-1.0d;
                    dVal = -dVal;
                }
                idx1++;
                int idx2=stVal.indexOf(' ',idx1);
                if((idx2>0)||(idx1<stVal.length())) {
                    if(idx2<=0) {
                        idx2=stVal.length();
                    }
                    dVal+= Integer.parseInt(stVal.substring(idx1,idx2))/60.0d;
                    idx2++;
                    if(idx2<stVal.length()) {
                        dVal+= Double.parseDouble(stVal.substring(idx2))/3600.0d;
                    }
                }
            } else {
                return false;
            }
            dVLat=dSign*dVal;
            
            stVal=miLon.getText().trim();
            //CourseLogAppForm.dbgL.addToLog(" lon :"+stVal);
            dVal=0.0d;
            dSign = 1.0d;
            idx1=stVal.indexOf(' ');
            if((idx1>0)||(stVal.length()>0)) {
                if(idx1<=0) {
                    idx1=stVal.length();
                }
                dVal+= Integer.parseInt(stVal.substring(0,idx1));
                if(dVal<0.0d) {
                    dSign=-1.0d;
                    dVal = -dVal;
                }
                idx1++;
                int idx2=stVal.indexOf(' ',idx1);
                if((idx2>0)||(idx1<stVal.length())) {
                    if(idx2<=0) {
                        idx2=stVal.length();
                    }
                    dVal+= Integer.parseInt(stVal.substring(idx1,idx2))/60.0d;
                    idx2++;
                    if(idx2<stVal.length()) {
                        dVal+= Double.parseDouble(stVal.substring(idx2))/3600.0d;
                    }
                }
            } else {
                return false;
            }
            dVLon=dSign*dVal;
            
            return true;
        } catch (NumberFormatException ex) {
            //CourseLogAppForm.dbgL.addToLog("\n"+ex);
            dVLat=courselog.gpsutils.GeoPoint.dummyAng;
            dVLon=courselog.gpsutils.GeoPoint.dummyAng;
            return false;
        }
    }
    
    /**
     * Compute Longitude from entered value.
     */
    public double getLongitudeValue() {
        return dVLon;
    }
    
    // OVERRIDDEN METHODS
    
    /**
     * Validate dialog inputs on OK.
     * @override ewe.ui.Form.onControlEvent(ControlEvent)
     */
    public void onControlEvent(ControlEvent ev) {
        if(ev.target==this.cbgChoice) {
            setDialogFromIndex();
            repaint();
        } else if((ev.target==this.ok)&&(ev.type==ControlEvent.PRESSED)) {
            //CourseLogAppForm.dbgL.addToLog(" ev :"+ev);
            if(convertValOK()) {
                super.onControlEvent(ev);
            }else{
                mtInfo.setText("Invalid value input");
                repaint();
            }
        } else {
            if((ev.target==this.miLat)||(ev.target==this.miLon)) {
                setDialogFromIndex();
            }
            super.onControlEvent(ev);
        }
    }
    
//    public void resize(int maxOrMin) {
//    }
    
    
}
